package com.pwc.conversionTool.soapUI;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.xmlbeans.XmlException;

import com.eviware.soapui.impl.WsdlInterfaceFactory;
import com.eviware.soapui.impl.wsdl.WsdlInterface;
import com.eviware.soapui.impl.wsdl.WsdlProject;
import com.eviware.soapui.impl.wsdl.WsdlRequest;
import com.eviware.soapui.impl.wsdl.WsdlSubmit;
import com.eviware.soapui.impl.wsdl.WsdlSubmitContext;
import com.eviware.soapui.model.iface.Operation;
import com.eviware.soapui.model.iface.Request;
import com.eviware.soapui.model.iface.Request.SubmitException;
import com.eviware.soapui.model.iface.Response;
import com.eviware.soapui.support.SoapUIException;

public class SoapUIClient {

	private WsdlProject project = null;

	private WsdlInterface iface = null;

	private Operation[] operations = null;

	private static String url = "";

	private WsdlRequest wsdlRequest = null;

	/**
	 * send in a url and generate a WSDL project,Iface
	 * 
	 * @param URL
	 */
	public void initialProject(String URL) {

		url = URL;
		try {
			WsdlProject currentProject = getProject();

			setIface(WsdlInterfaceFactory.importWsdl(currentProject, url, true)[0]);

			setOperations(iface.getAllOperations());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public String doSubmit(String OperationName, String RequestName, String payload) throws SubmitException {

		String ResponseTEXT = null;

		generateWSDLRequest(OperationName, RequestName, payload);

		WsdlSubmit submit = (WsdlSubmit) wsdlRequest.submit(new WsdlSubmitContext(wsdlRequest), false);

		Response response = (Response) submit.getResponse();

		ResponseTEXT = response.getContentAsString();

		return ResponseTEXT;
	}

	public String doSubmit(String OperationName, String payload) throws SubmitException {
		return doSubmit(OperationName, "Request 1", payload);
	}

	public String doSubmit() throws SubmitException {

		String ResponseTEXT = null;

		WsdlSubmit submit = (WsdlSubmit) wsdlRequest.submit(new WsdlSubmitContext(wsdlRequest), false);

		Response response = (Response) submit.getResponse();

		ResponseTEXT = response.getContentAsString();

		return ResponseTEXT;
	}

	/**
	 * 
	 * @param UserName
	 * @param Password
	 * @param SecurityType
	 */
	public void setSecurity(String UserName, String Password, String SecurityType) {
		wsdlRequest.setUsername(UserName);
		wsdlRequest.setPassword(Password);
		wsdlRequest.setWssPasswordType(SecurityType);
	}

	public void setSecurity(String UserName, String Password) {
		wsdlRequest.setUsername(UserName);
		wsdlRequest.setPassword(Password);
	}

	/**
	 * 
	 * @param OperationName
	 * @param RequestName
	 * @param payload
	 */
	public void generateWSDLRequest(String OperationName, String RequestName, String payload) {
		Operation operation = iface.getOperationByName(OperationName);

		WsdlRequest req = (WsdlRequest) operation.getRequestByName(RequestName);

		req.setRequestContent(payload);

		wsdlRequest = req;
	}

	public void generateWSDLRequest(String OperationName, String payload) {
		generateWSDLRequest(OperationName, "Request 1", payload);
	}

	/**
	 * 
	 * @return
	 */
	public Map<String, String> getOperationMap() {
		Map<String, String> result = new HashMap<String, String>();

		for (Operation o : operations) {
			String OperationName = o.getName();
			String template = o.getRequestList().get(0).getRequestContent();
			result.put(OperationName, template);
		}

		return result;
	}

	/**
	 * 
	 * @param operationName
	 * @return
	 */
	public String getTemplate(String operationName) {
		Map<String, String> map = getOperationMap();
		return map.get(operationName);
	}

	/**
	 * 
	 * @param OperationName
	 * @return
	 */
	public List<String> getRequestName(String OperationName) {
		List<String> requestNames = new ArrayList<String>();
		Operation operation = iface.getOperationByName(OperationName);

		for (Request req : operation.getRequestList()) {
			requestNames.add(req.getName());
		}

		return requestNames;
	}

	/**
	 * simple Singleton
	 * 
	 * @return
	 * @throws XmlException
	 * @throws IOException
	 * @throws SoapUIException
	 */
	public WsdlProject getProject() throws XmlException, IOException, SoapUIException {

		if (project != null) {
			return project;
		} else {
			project = new WsdlProject();

			return project;
		}
	}

	/**
	 * get string operation names
	 * 
	 * @return
	 */
	public List<String> getOperationsName() {
		List<String> operationNames = new ArrayList<String>();

		if (operations != null) {
			for (Operation operation : operations) {
				operationNames.add(operation.getName());
			}
		}
		return operationNames;
	}

	public WsdlInterface getIface() {
		return iface;
	}

	private void setIface(WsdlInterface iface) {
		this.iface = iface;
	}

	public Operation[] getOperations() {
		return operations;
	}

	private void setOperations(Operation[] operations) {
		this.operations = operations;
	}

}
